package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import com.yonyou.crm.bpub.detail.entity.*;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.AppCodeConstant;
import com.yonyou.crm.sys.modules.service.LayoutBizServiceImpl;

abstract public class BaseDetails {
	
	@Autowired
	LayoutBizServiceImpl layoutService;
	
	abstract public <T extends Serializable> T getDetail(long id);
	abstract public <T extends Serializable> List<DetailsGroupVO> getHeader(T obj, Map<String, List<DetailsGroupVO>> layoutList) throws CrmMessageException;
	abstract public <T extends Serializable> List<DetailsGroupVO> getBody(T obj, Map<String, List<DetailsGroupVO>> layoutList);
	abstract public <T extends Serializable> List<DetailsFlagVO> getFlag(T obj);
	abstract public <T extends Serializable> List<String> getAction(T obj);
	abstract public <T extends Serializable> Map<String, List<DetailsGroupVO>> getLayout(T obj);
	
	/**
	 * 获取header或body的模板
	 */
	public List<DetailsGroupVO> getLayout(Map<String, List<DetailsGroupVO>> layoutList, String name) {
		List<DetailsGroupVO> res = null;
		if ((layoutList != null) && (layoutList.size() != 0)) {
			if (layoutList.containsKey(name)) {
				res = layoutList.get(name);
			}
		}
		return res;
	}
	
	/**
	 * 将String格式转换为Date格式
	 * @param dateStr
	 * @return
	 * @throws CrmMessageException
	 */
	public Date transformDateFromStr(String dateStr, String dateFormate) throws CrmMessageException{
		String[] dateFormateArr = {"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm"};
		if (!Arrays.asList(dateFormateArr).contains(dateFormate)) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "日期格式化格式错误");
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormate);
		Date date;
		try{
			date = sdf.parse(dateStr);
		} catch(ParseException e) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "日期格式错误");
		}
		return date;
	}
	
	/**
	 * 格式化日期类型数据
	 * @param date
	 * @param dateFormate
	 * @return
	 * @throws CrmMessageException
	 */
	public String formateDate(Date date, String dateFormate) throws CrmMessageException{
		if (date == null) {
			return "";
		}
		String[] dateFormateArr = {"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm"};
		if (!Arrays.asList(dateFormateArr).contains(dateFormate)) {
			throw new CrmMessageException(AppCodeConstant.ServerError, "日期格式化格式错误");
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormate);
		return sdf.format(date);
	}
	
	/**
	 * 格式化金额类型数据
	 * @param price
	 * @return
	 */
	public String transformPrice(BigDecimal price) {
		if (price == null) {
			return "";
		}
		DecimalFormat df2 = new DecimalFormat("#,###.00");
        String priceStr = df2.format(price);
        return priceStr;
	}
	
	/**
	 * 地址信息
	 * @param customer
	 * @return
	 */
	public String getAddressInfo(String province, String city, String district, String street) {
		String address = "";
		if (province != null) {
			address += province;
		}
		if (city != null) {
			address += city;
		}
		if (district != null) {
			address += district;
		}
		if (street != null) {
			address += street;
		}
		return address;
	}
	
	/**
	 * 获取图片地址
	 * @param urlInfo
	 * @return
	 */
	public List<String> getPicUrl(Object urlInfo) {
		List<String> urlList = new ArrayList<>();
		if (urlInfo != null && !(urlInfo.equals(""))) {
			JSONArray jsonArray = JSONArray.fromObject(urlInfo); //把String转换为json
			List<Map<String, String>> urlInfoList = JSONArray.toList(jsonArray, new HashMap<String, String>(), new JsonConfig());
			for(Map<String, String> url : urlInfoList) {
				urlList.add(url.get("url"));
			}
		}
		return urlList;
	}
	
}
